package repositories

import (
	"Godzilla/common/convert"
	"Godzilla/common/db"
	"Godzilla/common/mysql"
	"Godzilla/models"
)

// ContactManager 用户跟群聊跟好友关系
type ContactManager struct {
	table		  	string
	userTable		string
	db				*db.DB
}

func NewContactManager() (ContactManager, error){
	db, err := db.NewDB("mysql", db.Dsn())
	if err != nil {
		return ContactManager{}, err
	}
	return ContactManager{
		table: 		  "contact",
		userTable:    "user",
		db:           db,
	}, err
}

func (manager *ContactManager)SearchGroupIds(userId int64) (groupIds []int64) {
	//TODO查找用户的全部群组
	var (
		sql		string
	)
	sql = "SELECT DISTINCT dst_id FROM " + manager.table + " WHERE owner_id = ? AND status = 1 AND chat_type = 2"
	rows, err := manager.db.Query(sql, userId)
	defer rows.Close()
	if err != nil {
		return
	}

	data := mysql.GetResultRows(rows)
	if len(data) == 0 {
		return
	}

	var contactArray []*models.Contact
	for i := 0; i < len(data); i++ {
		contact := &models.Contact{}
		convert.DataToStructByTagSql(data[i], contact)
		contactArray = append(contactArray, contact)
	}

	groupIds =make([]int64,0)
	for _, v := range contactArray{
		groupIds = append(groupIds, v.DstId)
	}

	return groupIds
}